﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.Runtime.Serialization;

namespace OpenComposite.Workflow
{
	[Designer(typeof(JumpJoinDesigner))]
	[ToolboxItem(typeof(JumpJoinToolboxItem))]
	[ToolboxBitmap(typeof(resfinder), "OpenComposite.Workflow.Resources.jumpjoin.PNG")]
	public partial class JumpJoinActivity : Activity
	{
		public JumpJoinActivity()
		{
			InitializeComponent();
		}

		protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
		{
			return base.Execute(executionContext);
		}
	}

	public class JumpJoinDesigner : ActivityDesigner
	{
		private const int width = 16;
		private const int height = 16;
		private readonly Size size = new Size(width, height);
		HorizontalWorkflowDesigner root = null;

		protected override void OnPaint(ActivityDesignerPaintEventArgs e)
		{
			//base.OnPaint(e);
			e.Graphics.FillEllipse(e.DesignerTheme.ForegroundBrush, Bounds);
		}

		protected override void OnDragOver(ActivityDragEventArgs e)
		{
			if ( e.Activities.Count == 1 && e.Activities[0] is JumpSplitActivity ) {
				if ( root == null )
					root = WorkflowHelpers.GetRootDesigner(this) as HorizontalWorkflowDesigner;

					if (root.DraggedActivity == null ||
						root.Workflow.WhileConnectors.Exists(delegate(FreeConnector conn)
						{
							if (conn.Start == e.Activities[0].QualifiedName)
								return true;
							else
								return false;
						})){
						e.Effect = System.Windows.Forms.DragDropEffects.None;

				} else {
					if ( this.ParentDesigner.Activity == e.Activities[0].Parent ||
						!WorkflowHelpers.HasInvalidJumpTarget(this) ) {
						e.Effect = System.Windows.Forms.DragDropEffects.Copy;
					} else {
						root.DraggedActivity = null;
						e.Effect = System.Windows.Forms.DragDropEffects.None;
					}
				}
			}
			base.OnDragOver(e);
		}

		protected override void OnDragDrop(ActivityDragEventArgs e)
		{
			/* Jump START*/
			if ( e.Activities.Count == 1 && e.Activities[0] is JumpSplitActivity ) {
				if ( root == null )
					root = WorkflowHelpers.GetRootDesigner(this) as HorizontalWorkflowDesigner;
				FreeConnector conn = new FreeConnector(
										root.DraggedActivity.QualifiedName,
										this.Activity.QualifiedName);
				conn.Level = this.Bounds.Top - 35;
				root.Workflow.WhileConnectors.Add(conn);

				return;
			}
			/* Jump END */

			base.OnDragDrop(e);
		}
		public override Size MinimumSize
		{
			get { return size; }
		}

		public override Size Size
		{
			get { return size; }
			set { base.Size = value; }
		}
	}

	[Serializable]
	public class JumpJoinToolboxItem : ActivityToolboxItem
	{
		public JumpJoinToolboxItem(Type type)
			: base(type)
		{
			this.DisplayName = "Jump (Join)";
		}

		private JumpJoinToolboxItem(SerializationInfo info, StreamingContext context)
		{
			Deserialize(info, context);
		}
	}

}
